package com.cloud.imgCloud.common;

import com.cloud.imgCloud.exception.ErrorCode;
import lombok.Getter;

import java.io.Serializable;


/**
 * 统一响应类
 */
@Getter
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private final int code;
    private final String message;
    private final T data;

    public R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public R(int code, T data) {
        this(code, "", data);
    }

    public R(ErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(200, data);
    }

    public static R<String> ok() {
        return ok("");
    }

    public static R<String> fail(String message) {
        return new R<>(0, message, null);
    }

    public static R<String> fail(ErrorCode errorCode) {
        return new R<>(errorCode);
    }
}
